/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Room;
import model.entities.RoomStatus;
import model.entities.RoomType;

/**
 *
 * @author Hieu.Nguyen
 */
public class RoomDAO {
    private static RoomDAO roomDAO;

    public static RoomDAO getInstance() {
        if (roomDAO == null) {
            roomDAO = new RoomDAO();
        }
        return roomDAO;
    }
    
   
     public List<Room> getRoom(boolean isActived) {
        List<Room> listRoom = new ArrayList<Room>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblRoom where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Room r = new Room();
                r.setRoomID(rs.getInt("RoomID"));
                r.setRoomNumber(rs.getString("RoomNumber"));
                
                RoomType aa = RoomTypeDAO.getInstance().getRoomTypeByID(rs.getInt("RoomTypeID"));                
                r.setRoomType(aa);
                RoomStatus rt = RoomStatusDAO.getInstance().getRoomStatusByID(rs.getInt("RoomStatusID"),true);                
                r.setRoomStatus(rt);
                
                
                r.setDescription(rs.getString("Description"));
                listRoom.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listRoom;
    }
     public boolean createRoom(Room room) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblRoom values (?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setString(1, room.getRoomNumber());
            pstmt.setInt(2, room.getRoomType().getRoomTypeID());
            pstmt.setInt(3, room.getRoomStatus().getRoomStatusID());
            pstmt.setString(4, room.getDescription());
            pstmt.setBoolean(5, room.isIsActived());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
      public Room getRoomByID(int roomID) {
        Room rm = new Room();
       
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblRoom where RoomID = ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, roomID);            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                rm.setRoomID(rs.getInt("RoomID"));
                rm.setRoomNumber(rs.getString("RoomNumber"));                
                RoomType rt = RoomTypeDAO.getInstance().getRoomTypeByID(rs.getInt("RoomTypeID"));
                rm.setRoomType(rt);
                
                RoomStatus roomst = RoomStatusDAO.getInstance().getRoomStatusByID(rs.getInt("RoomStatusID"),true);
                rm.setRoomStatus(roomst);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rm;
     }
      public boolean deleteRoom(int romID) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblRoom set isActived = 'false' where RoomID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, romID);

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }
      public boolean updateRoom(Room room) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblRoom set RoomNumber = ?, RoomTypeID = ?, RoomStatusID = ?, Description = ? where RoomID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, room.getRoomNumber());
            pstmt.setInt(2, room.getRoomType().getRoomTypeID());
            pstmt.setInt(3, room.getRoomStatus().getRoomStatusID());
            pstmt.setString(4, room.getDescription());
            pstmt.setInt(5, room.getRoomID());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
    }
}
